package com.example.uilearning;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SelectCityActivity extends ExpandableListActivity {

	
	//定义省份
	private String[] provinces={"广东","四川","陕西"};
	private String[][] cities={{"广州","佛山","潮州"},{"成都","南充","资阳"},{"西安","宝鸡","荆州"}};
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ExpandableListAdapter expandableListAdapter=new BaseExpandableListAdapter() {
			
			// 返回一个textview
			private TextView getTextView() {
							AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
									ViewGroup.LayoutParams.FILL_PARENT, 64);
							TextView textView = new TextView(
									SelectCityActivity.this);
							textView.setLayoutParams(lp);
							textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
							textView.setPadding(36, 0, 0, 0);
							textView.setTextSize(20);
							return textView;
						};
			@Override
			public boolean isChildSelectable(int groupPosition, int childPosition) {
				return true;
			}
			
			@Override
			public boolean hasStableIds() {
				return true;
			}
			
			@Override
			public View getGroupView(int groupPosition, boolean isExpanded,
					View convertView, ViewGroup parent) {
				LinearLayout ll=new LinearLayout(SelectCityActivity.this);
				ll.setOrientation(0);
				ImageView imageView=new ImageView(SelectCityActivity.this);
				ll.addView(imageView);
				TextView textView=getTextView();
				textView.setText(getGroup(groupPosition).toString());
				ll.addView(textView);
				return ll;
			}
			
			@Override
			public long getGroupId(int groupPosition) {
				return groupPosition;
			}
			
			@Override
			public int getGroupCount() {
				return provinces.length;
			}
			
			@Override
			public Object getGroup(int groupPosition) {
				return provinces[groupPosition];
			}
			
			@Override
			public int getChildrenCount(int groupPosition) {
				return cities[groupPosition].length;
			}
			
			@Override
			public View getChildView(int groupPosition, int childPosition,
					boolean isLastChild, View convertView, ViewGroup parent) {
				TextView textView=getTextView();
				textView.setText(getChild(groupPosition, childPosition).toString());
				return textView;
			}
			
			@Override
			public long getChildId(int groupPosition, int childPosition) {
				return groupPosition+childPosition;
			}
			
			@Override
			public Object getChild(int groupPosition, int childPosition) {
				return cities[groupPosition][childPosition];
			}
		};
		setListAdapter(expandableListAdapter);
		getExpandableListView().setOnChildClickListener(new OnChildClickListener() {
			
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				//获取启动该Activity之前的Activity对应的Intent
				Intent intent=getIntent();
				intent.putExtra("city", cities[groupPosition][childPosition]);
				//设置该selectCityActivity的结果码，并设置结束之后退回的Activity
				SelectCityActivity.this.setResult(0,intent);
				SelectCityActivity.this.finish();
				return false;
			}
		});
	}
	

}
